<template>
	<view :style="themeColor">
		<view class="page">
			<view class="flex flex-wrap align-center justify-center benben-position-layout flex passwordLogin_flex_0"
				:style="{height:(88+StatusBarRpx)+'rpx',paddingTop:StatusBarRpx+'rpx', }">
				<text class='passwordLogin_fd0_0'>登录</text>

			</view>
			<view :style="{height: (88+StatusBarRpx)+'rpx'}"></view>
			<!---logoflex布局开始-->
			<view class="flex flex-direction flex-wrap align-center justify-center benben-flex-layout passwordLogin_flex_1">
				<image class='passwordLogin_fd1_0' mode="aspectFill" src='/static/images/logo.png'></image>
				<text class='passwordLogin_fd1_1'>码上约</text>
			</view>

			<!---logoflex布局结束-->
			<!---flex布局flex布局开始-->

			<view class="flex flex-direction flex-wrap align-stretch benben-flex-layout passwordLogin_flex_2"
				v-if="isShouquan==1">
				<view class='flex flex-wrap align-center justify-center passwordLogin_fd2_0' @tap.stop="LoginFunc">
					<text class='passwordLogin_fd2_0_c1'>登录</text>
				</view>
			</view>
			<button class="login-btn" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber"
				v-if="isShouquan==2">授权手机号码</button>
<!-- 			<button class="login-btn" open-type="chooseAvatar" @chooseavatar="onGetUserInfo">
				授权获取用户信息
			</button> -->

			<!---flex布局flex布局结束-->
			<!-- 			<view class="flex flex-wrap align-center justify-center benben-position-layout flex passwordLogin_flex_3">
				<benben-flex-switch-new class-name='flex flex passwordLogin_fd3_0' v-model="is_tick" :disabled='false'>
					<template v-slot:checked>

						<view class='flex flex position-relative align-center'>
							<image class='passwordLogin_checkfd3_0_c0_c0' mode="aspectFit" :src='STATIC_URL+"126.png"'>
							</image>
						</view>

					</template>
					<template v-slot:unchecked>

						<view class='flex flex position-relative justify-end align-center'>
							<image class='passwordLogin_noCheckfd3_0_c1_c0' mode="aspectFit" :src='STATIC_URL+"127.png"'>
							</image>
						</view>

					</template>
				</benben-flex-switch-new>
				<text class='passwordLogin_fd3_1'>{{$t('已阅读并同意')}}</text>
				<text class='passwordLogin_fd3_2' @tap.stop="handleJumpDiy" data-type="navigateTo"
					:data-url="`/pages/myB/agreement/agreement?type=1`">{{$t('《用户注册协议》')}}</text>
				<text class='passwordLogin_fd3_1'>{{$t('与')}}</text>
				<text class='passwordLogin_fd3_4' @tap.stop="handleJumpDiy" data-type="navigateTo"
					:data-url="`/pages/myB/agreement/agreement?type=2`">《隐私政策》</text>

			</view> -->
			<view :style="{height: '88rpx'}"></view>


		</view>
	</view>
</template>
<script>
	import {
		CryptoencryptedData
	} from '@/common/utils/getPhone.js';
	// import {
	// 	v4 as uuidv4
	// } from 'uuid';

	export default {
		components: {

		},


		data() {
			return {
				"is_tick": false,
				uuid: '',
				openid: '',
				wxList: '',
				isShouquan: 1,
				info: {
					code: ''
				},
				invite_code: '',
				gzhopId: '',
				sessionKey: '',
				isLoginClicked: false, // 用于防抖的标志
			};
		},
		computed: {
			themeColor() {
				return this.$store.getters.themeColor
			},

		},
		watch: {},
		async onLoad(options) {

		},
		onUnload() {

		},
		onReady() {

		},
		onShow() {

		},
		onHide() {

		},
		onResize() {

		},
		onPullDownRefresh() {

		},
		onReachBottom(e) {

		},
		onPageScroll(e) {

		},
		methods: {

			// 微信登录
			LoginFunc() {

				let self = this
				if (self.isLoginClicked) {
					return; // 如果已点击，则不执行后续代码
				}

				// 设置已点击标志为true
				self.isLoginClicked = true;

				// 1秒后重置点击标志
				setTimeout(() => {
					self.isLoginClicked = false;
				}, 1000);
				console.log('测试防抖')
				// 获取微信用户信息
				uni.getUserProfile({
					desc: '用于完善会员资料',
					success: function(infoRes) {
						self.infoRes = infoRes
						uni.showLoading({
							title: '登录中~'
						})
						console.log(infoRes, '~~~~~~~~~iv和encryptedData');
						// uni.setStorageSync('iv', infoRes.iv); // 保存iv
						// uni.setStorageSync('encryptedData', infoRes.encryptedData); // 保存encryptedData
						self.wxGetCode();
					},
					fail: function(err) {
						this.$message.info('ProfileErr', err);
						console.log(err, 'errrrrrrrrrrrrrrrrrrrrr错误信息')
					},

				});
			},
			//获取code
			wxGetCode() {
				wx.login({
					provider: 'weixin',
					success: loginRes => {
						uni.setStorageSync('code', loginRes.code); // 保存wxcode
						console.log(loginRes, '微信小程序登录code');
						this.userLogin(loginRes.code)
					},
					fail: err => {
						// this.$msg(this, {
						// 	title: '获取用户code失败',
						// 	type: 'warning'
						// });
						this.$message.info('codeErr', err);
						uni.removeStorageSync('code');
					}
				});
			},
			// //判断用户是否存在  然后携带iv code encry跳转
			async userLogin(code) {
				uni.showLoading({
					title: '登录中...'
				})

				try {
					// let iv = uni.getStorageSync('iv');
					// let encryptedData = uni.getStorageSync('encryptedData');
					// let uuid = uuidv4();
					// console.log(uuid, '这是uuid');

					// console.log(iv, 'iiiiiivvvvvv')
					// console.log(code, 'codecodecode')
					// console.log(encryptedData, 'encryptedDataencryptedData')

					// let requestUrl = `${global.apiUrls.wxlogin}/${code}`;
					// let datauserInfo = await this.$api.get(requestUrl);
					const res = await this.$api.get(global.apiUrls.wxlogin, {
						code: code
					});
					console.log(res, '登录判断成功返回的回调信息')
					if (res.data.code == 200) {

						if(res.data.data.token){
							let token = res.data.data.token
							let userInfo = res.data.data
							console.log(token, '######$$$$$$$');
							console.log(userInfo, '######$$$$$$$');
							this.$store.commit('saveToken', token)
							this.$store.commit('updateUserInfo', userInfo)
							setTimeout(() => {
								this.$urouter.switchTab(`/pages/tabBar/homePage/homePage`);
							}, 800)
							return
						}
						this.openid = res.data.data.openid
						this.uuid = res.data.data.uuid

						uni.hideLoading()
						this.$message.info('请授权手机号');
						this.isShouquan = 2
					} else {
						this.$message.info(res.data.msg);
						this.isShouquan = 1
						return
					}
				} catch (err) {
					this.$message.info("catchErr", err);
					console.log(err, '错误')
					uni.hideLoading()
				}
			},

			async onGetPhoneNumber(e) {

				console.log(e, '授权手机号参数')
				try {
					// let iv = uni.getStorageSync('iv');
					// let encryptedData = uni.getStorageSync('encryptedData');
					// let uuid = uuidv4();
					// console.log(uuid, '这是uuid');

					// console.log(iv, 'iiiiiivvvvvv')
					// console.log(code, 'codecodecode')
					// console.log(encryptedData, 'encryptedDataencryptedData')

					// let requestUrl = `${global.apiUrls.wxlogin}/${code}`;
					// let datauserInfo = await this.$api.get(requestUrl);
					const res = await this.$api.post(global.apiUrls.loginSS, {
						encryptedData: e.detail.encryptedData,
						iv: e.detail.iv,
						openid: this.openid,
						uuid: this.uuid
					});
					console.log(res, '登录判断成功返回的回调信息')
					if (res.data.code == 200) {
						let token = res.data.data.token
						let userInfo = res.data.data
						console.log(token, '######$$$$$$$');
						console.log(userInfo, '######$$$$$$$');
						this.$store.commit('saveToken', token)
						this.$store.commit('updateUserInfo', userInfo)
						// this.sessionKey = res.data.data.sessionKey
						setTimeout(() => {
							this.$urouter.switchTab(`/pages/tabBar/homePage/homePage`);
						}, 800)
					} else {
						this.$message.info(res.data.msg);
						this.isShouquan = 1
						return
					}
				} catch (err) {

				}
			},
			async onGetUserInfo(e) {
				console.log(e, '获取到的加密数据'); // 获取到的加密数据
			},
			// async onGetPhoneNumber(e) {
			// 	console.log(e, '获取到的加密数据'); // 获取到的加密数据
			// 	let iv = e.detail.iv
			// 	let encryptedData = e.detail.encryptedData
			// 	let sessionKey = this.sessionKey
			// 	console.log(sessionKey, '塞神k');

			// 	let jiexi = CryptoencryptedData(encryptedData, iv, sessionKey)
			// 	let keyPhone = jiexi.phoneNumber
			// 	console.log(keyPhone, 'keyPhonekeyPhone')
			// 	uni.setStorageSync('keyPhone', keyPhone)
			// 	setTimeout(() => {
			// 		this.$urouter.switchTab(`/pages/tabBar/homePage/homePage`);
			// 	}, 800)
			// },
		}
	};
</script>
<style lang="scss" scoped>
	.page {
		width: 100vw;
		overflow-x: hidden;
		min-height: calc(100vh - var(--window-bottom));
		background: #fff;
		background-size: 100% auto;
	}

	.login-btn {
		display: flex;
		justify-content: center;
		align-items: center;
		// line-height: 90rpx;
		// text-align: center;
		width: 646rpx;
		height: 88rpx;
		// margin-top: 35rpx;
		margin-left: 54rpx;
		background: #167deb;
		font-size: 32rpx;
		color: #FFFFFF;
		// background: linear-gradient(90deg, #FF840E 0%, #FD5702 100%);
		// background-blend-mode: normal, normal;
		border-radius: 44rpx;
	}

	.passwordLogin_flex_0 {
		background: #fff;
		width: 750rpx;
		height: 88rpx;
		overflow: hidden;
		z-index: 10;
		top: 0rpx;
		background-size: 100% auto !important;
	}

	.passwordLogin_fd0_0 {
		font-size: 36rpx;
		font-weight: 500;
		color: #333333;
		line-height: 50rpx;
	}

	.passwordLogin_flex_1 {
		padding: 92rpx 0rpx 314rpx 0rpx;
	}

	.passwordLogin_flex_10 {
		padding: 22rpx 0rpx 14rpx 0rpx;
	}

	.passwordLogin_fd1_1 {
		color: #000000;
		font-size: 32rpx;
		font-weight: 400;
		line-height: 50rpx;
	}

	.passwordLogin_fd1_10 {
		color: #167deb;
		font-size: 32rpx;
		font-weight: 400;
		line-height: 50rpx;
	}

	.passwordLogin_fd1_0 {
		width: 180rpx;
		height: 180rpx;
		border-radius: 48rpx 48rpx 48rpx 48rpx;
		margin: 0rpx 0rpx 32rpx 0rpx;
	}

	.passwordLogin_flex_2 {
		padding: 0rpx 40rpx 0rpx 40rpx;
	}

	.passwordLogin_fd2_0_c1 {
		color: #FFFFFF;
		font-size: 32rpx;
		font-weight: 500;
		line-height: 45rpx;
	}

	.passwordLogin_fd2_0_c0 {
		width: 44rpx;
		height: 36rpx;
		margin: 0rpx 16rpx 0rpx 0rpx;
	}

	.passwordLogin_fd2_01 {
		width: 638rpx;
		height: 88rpx;
		margin: 0rpx 16rpx 0rpx 16rpx;
	}

	.passwordLogin_fd2_0 {
		background: #167deb;
		border-radius: 44rpx 44rpx 44rpx 44rpx;
		width: 638rpx;
		height: 88rpx;
		margin: 0rpx 16rpx 0rpx 16rpx;
	}

	.passwordLogin_flex_3 {
		background: #fff;
		width: 750rpx;
		height: 88rpx;
		overflow: hidden;
		z-index: 10;
		bottom: calc(0rpx + var(--window-bottom));
		background-size: 100% auto !important;
	}

	.passwordLogin_fd3_4 {
		color: rgba(240, 102, 0, 1);
		font-size: 24rpx;
		font-weight: 400;
	}

	.passwordLogin_fd3_2 {
		font-size: 24rpx;
		font-weight: 400;
		color: rgba(240, 102, 0, 1);
	}

	.passwordLogin_fd3_1 {
		font-size: 24rpx;
		font-weight: 400;
		color: rgba(153, 153, 153, 1);
	}

	.passwordLogin_noCheckfd3_0_c1_c0 {
		border-radius: 12rpx 12rpx 12rpx 12rpx;
		width: 32rpx;
		height: 32rpx;
		margin: 0rpx 0rpx 0rpx 0rpx;
	}

	.passwordLogin_checkfd3_0_c0_c0 {
		width: 32rpx;
		height: 32rpx;
		border-radius: 12rpx 12rpx 12rpx 12rpx;
		margin: 0rpx 0rpx 0rpx 0rpx;
	}

	::v-deep .passwordLogin_fd3_0 {
		margin: 0rpx 16rpx 0rpx 0rpx;
	}
</style>